class Stat
  @@cpu_counter_names = %w{
    user
    nice
    system
    idle
    iowait
    irq
    softirq
    steal
    guest
  }
  def initialize()
    @stat = Hash.new
    stat = IO.readlines("/proc/stat")
    stat.each do |line|
      fields = line.split(' ')
      name = fields.shift
      if (name =~ /^cpu/)
        @@cpu_counter_names.each do |counter_name|
          @stat["#{name}_#{counter_name}"] = fields.shift
        end
      elsif (name =~ /^intr/)
        @stat["irq_total"] = fields.shift # all IRQs
        irq_number = 0 # IRQ number
        fields.each do |count|
          @stat["irq_#{irq_number.to_s}"] = fields.shift
          irq_number += 1
        end
      else
        @stat[name] = fields.shift
      end
    end
  end
end

